/*
 * @Description: 
 * @version: 
 * @Author: cy
 * @Date: 2021-07-19 10:56:46
 * @LastEditors: cy
 * @LastEditTime: 2021-07-19 11:53:58
 */
export default class StackObj {
    constructor() {
        this.count = 0;
        this.items = {};
    }
    push(element) {
        this.items[this.count] = element;
        this.count++;
    }
    // 验证栈是否为空
    isEmpty() {
        return this.count === 0;
    }
    // 验证栈的长度
    size() {
        return this.count;
    }
    // 从栈中弹出元素
    pop() {
        if (this.isEmpty()) {
            return undefined;
        }
        this.count --;
        const result = this.items[this.count];
        delete this.items[this.count];
        return result;
    }
    // 查看栈顶值
    peek() {
        if (this.isEmpty()) {
            return undefined;
        }
        return this.items[this.count - 1];
    }
    indexOf(item) {
        Object.keys(this.items).find(k => compare(this.items[k], item));
    }
    lastIndexOf(item) {
        
    }
    getChildOfIndex($index) {
        if (this.isEmpty()) {
            return undefined;
        }
        return this.items[$index];
    }
    clear() {
        this.items = {};
        this.count = 0;
        // 可以遵循LIFO移除所有元素
        while (!this.isEmpty()) {
            this.pop();
        }
    }
    toString() {
        console.log('toString')
        if (this.isEmpty()) {
            return '';
        }
        let objString = `${this.items[0]}`;
        for (let i = 1; i < this.count; i++) {
            objString = `${objString}, ${this.items[i]}`;
        }
        return objString;
    }
}
